<?php

namespace frontend\controllers;

use bc\controller\GameController;
use bc\model\PlatoModel;
use bc\util\Util;

class PlatoController extends GameController
{
	protected $device;
	protected $shareContent = '没想到你是这样的宝宝';
	protected $shareDesc = '怎么也没想到。。。';
	protected $shareUrl = 'http://wx.zhishimama.com/plato';
	
	/**
	 *
	 * {@inheritdoc}
	 *
	 * @see \bc\controller\FrontController::init()
	 */
	public function init()
	{
		parent::init ();
		$this->device = Util::getBroswer ();
	}
	
	/**
	 */
	public function actionIndex()
	{
		$view = [ ];
		
		if ($this->device == 'wechat')
		{
			// 生成js sdk签名
			$jsConfig = $this->configJsApi ();
			$appid = $this->wechat ()->appid;
			$secret = $this->wechat ()->secret;
			$view ['share_url'] = $this->shareUrl;
			$view ['share_content'] = $this->shareContent;
			$view ['share_desc'] = $this->shareDesc;
			$view ['js_config'] = $jsConfig;
		}
		
		$view ['device'] = $this->device;
		
		return $this->render ( 'index', $view );
	}
	
	/**
	 */
	public function actionSave()
	{
		$data ['nickname'] = $this->post ( 'nickname' );
		$birthday = $this->post ( 'birthday' );
		$birthday = date ( 'Y-m-d', strtotime ( $birthday ) );
		$data ['birthday'] = $birthday;
		$data ['create_time'] = time ();
		$data ['share_count'] = 0;
		
		if ($this->device == 'wechat')
		{
			$data ['device'] = 1;
		}
		else if ($this->device == 'Weibo')
		{
			$data ['device'] = 2;
		}
		else
		{
			$data ['device'] = 3;
		}
		
		$model = new PlatoModel ();
		$id = $model->add ( $data );
		
		$this->redirect ( '/plato/result?id=' . $id );
	}
	
	/**
	 */
	public function actionResult()
	{
		if ($this->device == 'wechat')
		{
			// 生成js sdk签名
			$jsConfig = $this->configJsApi ();
			$appid = $this->wechat ()->appid;
			$secret = $this->wechat ()->secret;
			$view ['share_url'] = $this->shareUrl;
			$view ['share_content'] = $this->shareContent;
			$view ['share_desc'] = $this->shareDesc;
			$view ['js_config'] = $jsConfig;
		}
		
		$id = $this->get ( 'id' );
		
		$model = new PlatoModel ();
		$row = $model->getRowById ( $id );
		
		if (! $row)
			$this->redirect ( '/plato' );
		
		$birthday_arr = explode ( '-', $row ['birthday'] );
		
		$day = $birthday_arr [1] . $birthday_arr [2];
		
		$tag_image = '/assets/tinified/' . $day . '.png';
		
		$row ['tag_image'] = $tag_image;
		$view ['row'] = $row;
		
		$view ['device'] = $this->device;
		
		
		return $this->render ( 'result', $view );
	}
	
	
	/**
	 * 
	 */
	public function actionShare()
	{
		$id = $this->post('id');
		$model = new PlatoModel ();
		$row = $model->getRowById ( $id );
		if($row)
		{
			$data['share_count'] = $row['share_count'] + 1;
			$model->modify($data, $id);
		}
	}
	
	public function actionImage()
	{
		$id = $this->post('id');
		$img = $this->post('image_code');
		
		$img = str_replace('data:image/jpeg;base64,', '', $img);
		$img = str_replace(' ', '+', $img);
		$data = base64_decode($img);
		
		$file_name = 'plato/'.uniqid() . '.jpg';
		
		$imagePath = UPLOAD_PATH . $file_name;
		
// 		$model = new PlatoModel ();
// 		$data['image'] = $file_name;
// 		$model->modify($data, $id);
		
		
		$rs = file_put_contents($imagePath, $data);
		
		echo json_encode(array('code' => $rs, 'image_path' => STATIC_IMAGE . $file_name));
		exit;
	}
}

